package boot;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@ComponentScan("rewardsonline")
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

	/**
	 * Resolves messages (used to support internationalization and to avoid 
	 * hard-coded strings in view pages). Don't worry about this for now, we will 
	 * cover message-sources later.
	 */
	// TODO-16: Optional Step - read the last section of the detailed instructions 
	// in the lab-notes. Comment this bean out when told to do so.
	@Bean
	public MessageSource messageSource() {
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setCacheSeconds(0);
		messageSource.setBasename("classpath:messages/global");
		return messageSource;
	}

	
	// TODO-04: Define a view-resolver to convert logical view names to JSPs. 
	// Modify your WelcomeController method to return the logical-view "welcome".
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.jsp("/WEB-INF/", ".jsp");
	}

	
	/**
	 * Map URL /resources/* to serve static resources from classpath:/static/*
	 * This allows us to store and distribute css, images, etc. in JAR file.
	 * This is the equivalent of <mvc:resources/>
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/");
	}

}
